package com.chunfeng.lerunapplication.api;

import android.content.Context;
import com.chunfeng.lerunapplication.entity.JsonRequest;
import com.chunfeng.lerunapplication.entity.Orders;
import com.chunfeng.lerunapplication.utils.CacheUtils;
import com.chunfeng.lerunapplication.utils.HttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单API
 *
 * @author by 春风能解释
 * <p>
 * 2023/11/27
 */
public class OrderApi {

    /**
     * 应用上下文
     */
    private Context context;

    public OrderApi(Context context) {
        this.context = context;
    }

    /**
     * 按条件获取订单信息
     *
     * @param orders 条件
     * @return JSON
     */
    public JsonRequest<List<Orders>> lookOrdersByCondition(Orders orders) {
        Map<String, String> token = new HashMap<>();
        if (getToken(token)) {
            return null;
        }
        return new HttpUtils<List<Orders>>().sendHttpRequest(
                ApiEnum.LOOK_ORDER.getUrl(),
                ApiEnum.LOOK_ORDER.getMethod(),
                orders,
                token
                , null
        );
    }

    /**
     * 新增一条订单信息
     *
     * @param orders 订单信息
     * @return JSON
     */
    public JsonRequest<Integer> insertOneOrder(Orders orders) {
        Map<String, String> token = new HashMap<>();
        if (getToken(token)) {
            return null;
        }
        return new HttpUtils<Integer>().sendHttpRequest(
                ApiEnum.INSERT_ORDER.getUrl(),
                ApiEnum.INSERT_ORDER.getMethod(),
                orders,
                token,
                null
        );
    }

    /**
     * 修改一条订单信息
     *
     * @param orders 订单信息
     * @return JSON
     */
    public JsonRequest<Integer> updateOneOrders(Orders orders) {
        Map<String, String> token = new HashMap<>();
        if (getToken(token)) {
            return null;
        }
        return new HttpUtils<Integer>()
                .sendHttpRequest(
                        ApiEnum.UPDATE_ORDER.getUrl(),
                        ApiEnum.UPDATE_ORDER.getMethod(),
                        orders,
                        token,
                        null
                );
    }

    /**
     * 删除订单信息
     *
     * @param ids ID值
     * @return JSON
     */
    public JsonRequest<Integer> deleteOrders(Integer[] ids) {
        Map<String, String> token = new HashMap<>();
        if (getToken(token)) {
            return null;
        }
        return new HttpUtils<Integer>()
                .sendHttpRequest(
                        ApiEnum.DELETE_ORDER.getUrl(),
                        ApiEnum.DELETE_ORDER.getMethod(),
                        ids,
                        token,
                        null
                );
    }

    /**
     * 获取Token
     *
     * @param header Map集合
     * @return 是否为空
     */
    private boolean getToken(Map<String, String> header) {
        String token = new CacheUtils(context).getValue("token");
        if (token.isEmpty()) {
            return true;
        }
        header.put("token", token);
        return false;
    }

}
